package com.sky.controller.admin;

import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工相关接口")
@Slf4j
@Component
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    /**
     * 登录
     *
     * @param employeeLoginDTO dto
     * @return result
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        return Result.success(employeeLoginVO);
    }

    /**
     * 新增员工
     *
     * @param employeeDTO dto
     * @return result
     */
    @ApiOperation("新增员工")
    @PostMapping
    public Result<String> add(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工:{}", employeeDTO);
        employeeService.add(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页条件查询
     *
     * @param employeePageQueryDTO dto
     * @return R
     */
    @GetMapping("/page")
    public Result<PageBean> pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工分页查询:{}", employeePageQueryDTO);
        PageBean pageBean = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageBean);
    }

    /**
     * 启用、禁用员工账号
     *
     * @param status 员工状态
     * @param id     员工id
     * @return R
     */
    @PostMapping("/status/{status}")
    public Result<String> changeStatus(@PathVariable Integer status, Long id) {
        log.info("启用、禁用员工账号:{},{}", status, id);
        employeeService.changeStatus(status, id);
        return Result.success();
    }

    /**
     * 根据id查询员工
     *
     * @param id 员工id
     * @return R
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     *
     * @param employeeDTO dto
     * @return R
     */
    @PutMapping
    public Result<String> update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("编辑员工信息：{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }


    /**
     * 退出登录
     *
     * @return result
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        employeeService.logout();
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO 修改密码dto
     * @return R
     */
    @PutMapping("/editPassword")
    public Result<String> editPassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }
}
